package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.Flucard_IT.Home;
import com.Flucard_IT.SplashScreen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import define.Constants;

public class CheckFlucardAsync extends AsyncTask<Integer, String, Boolean> {

	public boolean CONNECTION_RESULT = false;

	private String TAG = CheckFlucardAsync.class.getSimpleName();

	private Context context;

	public CheckFlucardAsync(Context context) {
		this.context = context;
	}

	@Override
	protected Boolean doInBackground(Integer... params) {
		boolean isWifi = ((ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE))
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
		/**
		 * Check wireless was turned on or not
		 */
		if (!isWifi) {
			SplashScreen.IS_ENTER_CHECK_CONNECTION_HANDLER = true;
		} else {
			SplashScreen.IS_ENTER_CHECK_CONNECTION_HANDLER = false;
		}

		CONNECTION_RESULT = connectToServer();

		return false;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		Constants.utils.hideProgressDialog();

		/**
		 * If connected right SSID Name again, open this app again
		 */
		if (!SplashScreen.IS_ENTER_CHECK_CONNECTION_HANDLER & CONNECTION_RESULT) {
			SplashScreen.IS_DISABLE_HANDLER = true;

			((Activity) context).finish();
			context.startActivity(new Intent(context, Home.class));
		}

		/**
		 * If connected to wrong SSID Name, Handler still do nothing
		 */
		if (!SplashScreen.IS_ENTER_CHECK_CONNECTION_HANDLER
				& !CONNECTION_RESULT) {
			SplashScreen.IS_ENTER_CHECK_CONNECTION_HANDLER = true;
		}
	}

	private boolean connectToServer() {
		boolean relt = false;

		try {
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
			HttpConnectionParams.setSoTimeout(httpParams, 5000);
			HttpClient client = new DefaultHttpClient(httpParams);

			HttpGet request = new HttpGet("http://" + Constants.IP);
			HttpResponse response = client.execute(request);
			InputStream in = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String line = null;
			while ((line = reader.readLine()) != null) {
				if (line.indexOf(Constants.XML_INDEX) != -1) {
					relt = true;
					break;
				}
			}
			in.close();
			return relt;
		} catch (MalformedURLException e) {
			return relt;
		} catch (IOException e) {
			return relt;
		}
	}
}
